import {TaskRepository} from '../repository/task';
import {Task} from '../entity/task';
import {getRepository} from "typeorm";

// 获取存储库
// @Injectable()
class TaskService {

    private taskRepository: TaskRepository;

    constructor() {
        // this.taskRepository = new TaskRepository(Task, manager);
        this.taskRepository = getRepository(Task);
    }

    async findAll(): Promise<Task[]> {
        return this.taskRepository.find(
            {
                relations: {
                    user: true,
                }
            }
        );
    }

    async findOne(id: number): Promise<Task | null> {
        return this.taskRepository.findOneBy({"id": id});
    }

    async create(data: object): Promise<Task> {
        const task = this.taskRepository.create(data);
        return this.taskRepository.save(task);
    }

    async update(id: number, data: object): Promise<Task | undefined> {
        return this.taskRepository.save({id, ...data})
    }

    async delete(id: number): Promise<void> {
        this.taskRepository.delete(id);
    }
}

export default TaskService;
